home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / RandomAccessFile.java < prev    next >
Text File  |  1998-09-22  |  32KB  |  862 lines

  1. /*
  2.  * @(#)RandomAccessFile.java    1.34 98/07/01
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.io;
  16.  
  17. import java.io.File;
  18.  
  19. /**
  20.  * Instances of this class support both reading and writing to a 
  21.  * random access file. An application can modify the position in the 
  22.  * file at which the next read or write occurs. 
  23.  * This class provides a sense of security
  24.  * by offering methods that allow specified mode accesses of 
  25.  * read-only or read-write to files.
  26.  *
  27.  * @author  unascribed
  28.  * @version 1.34, 07/01/98
  29.  * @since   JDK1.0
  30.  */
  31. public
  32. class RandomAccessFile implements DataOutput, DataInput {
  33.     private FileDescriptor fd;
  34.  
  35.     /**
  36.      * Creates a random access file stream to read from, and optionally 
  37.      * to write to, a file with the specified name. 
  38.      * <p>
  39.      * The mode argument must either be equal to <code>"r"</code> or 
  40.      * <code>"rw"</code>, indicating either to open the file for input or 
  41.      * for both input and output. 
  42.      *
  43.      * @param      name   the system-dependent filename.
  44.      * @param      mode   the access mode.
  45.      * @exception  IllegalArgumentException  if the mode argument is not equal
  46.      *               to <code>"r"</code> or to <code>"rw"</code>.
  47.      * @exception  IOException               if an I/O error occurs.
  48.      * @exception  SecurityException         if a security manager exists, its
  49.      *               <code>checkRead</code> method is called with the name
  50.      *               argument to see if the application is allowed read access
  51.      *               to the file. If the mode argument is equal to
  52.      *               <code>"rw"</code>, its <code>checkWrite</code> method also
  53.      *               is called with the name argument to see if the application
  54.      *               is allowed write access to the file. Either of these may
  55.      *               result in a security exception.
  56.      * @see        java.lang.SecurityException
  57.      * @see        java.lang.SecurityManager#checkRead(java.lang.String)
  58.      * @since      JDK1.0
  59.      */
  60.     public RandomAccessFile(String name, String mode) throws IOException {
  61.     boolean rw = mode.equals("rw");
  62.     if (!rw && !mode.equals("r"))
  63.         throw new IllegalArgumentException("mode must be r or rw");
  64.     SecurityManager security = System.getSecurityManager();
  65.     if (security != null) {
  66.         security.checkRead(name);
  67.         if (rw) {
  68.         security.checkWrite(name);
  69.         }
  70.     }
  71.     fd = new FileDescriptor();
  72.     open(name, rw);
  73.     }
  74.     
  75.     /**
  76.      * Creates a random access file stream to read from, and optionally 
  77.      * to write to, the file specified by the <code>File</code> argument. 
  78.      * <p>
  79.      * The mode argument must either be equal to <code>"r"</code> or to 
  80.      * <code>"rw"</code>, indicating either to open the file for input, 
  81.      * or for both input and output, respectively. 
  82.      *
  83.      * @param      file   the file object.
  84.      * @param      mode   the access mode.
  85.      * @exception  IllegalArgumentException  if the mode argument is not equal
  86.      *               to <code>"r"</code> or to <code>"rw"</code>.
  87.      * @exception  IOException               if an I/O error occurs.
  88.      * @exception  SecurityException         if a security manager exists, its
  89.      *               <code>checkRead</code> method is called with the pathname
  90.      *               of the <code>File</code> argument to see if the
  91.      *               application is allowed read access to the file. If the
  92.      *               mode argument is equal to <code>"rw"</code>, its
  93.      *               <code>checkWrite</code> method also is called with the
  94.      *               pathname to see if the application is allowed write access
  95.      *               to the file.
  96.      * @see        java.io.File#getPath()
  97.      * @see        java.lang.SecurityManager#checkRead(java.lang.String)
  98.      * @since      JDK1.0
  99.      */
  100.     public RandomAccessFile(File file, String mode) throws IOException {
  101.     this(file.getPath(), mode);
  102.     }
  103.  
  104.     /**
  105.      * Returns the opaque file descriptor object associated with this stream.
  106.      *
  107.      * @return     the file descriptor object associated with this stream.
  108.      * @exception  IOException  if an I/O error occurs.
  109.      * @see        java.io.FileDescriptor
  110.      * @since      JDK1.0
  111.      */
  112.     public final FileDescriptor getFD() throws IOException {
  113.     if (fd != null) return fd;
  114.     throw new IOException();
  115.     }
  116.  
  117.     /**
  118.      * Opens a file and returns the file descriptor.  The file is 
  119.      * opened in read-write mode if writeable is true, else 
  120.      * the file is opened as read-only.
  121.      * @param name the name of the file
  122.      * @param writeable the boolean indicating whether file is 
  123.      * writeable or not.
  124.      */
  125.     private native void open(String name, boolean writeable) throws IOException;
  126.  
  127.     // 'Read' primitives
  128.     
  129.     /**
  130.      * Reads a byte of data from this file. This method blocks if no 
  131.      * input is yet available. 
  132.      *
  133.      * @return     the next byte of data, or <code>-1</code> if the end of the
  134.      *             file is reached.
  135.      * @exception  IOException  if an I/O error occurs.
  136.      * @since      JDK1.0
  137.      */
  138.     public native int read() throws IOException;
  139.  
  140.     /**
  141.      * Reads a sub array as a sequence of bytes. 
  142.      * @param b the data to be written
  143.      * @param off the start offset in the data
  144.      * @param len the number of bytes that are written
  145.      * @exception IOException If an I/O error has occurred.
  146.      */
  147.     private native int readBytes(byte b[], int off, int len) throws IOException;
  148.  
  149.     /**
  150.      * Reads up to <code>len</code> bytes of data from this file into an 
  151.      * array of bytes. This method blocks until at least one byte of input 
  152.      * is available. 
  153.      *
  154.      * @param      b     the buffer into which the data is read.
  155.      * @param      off   the start offset of the data.
  156.      * @param      len   the maximum number of bytes read.
  157.      * @return     the total number of bytes read into the buffer, or
  158.      *             <code>-1</code> if there is no more data because the end of
  159.      *             the file has been reached.
  160.      * @exception  IOException  if an I/O error occurs.
  161.      * @since      JDK1.0
  162.      */
  163.     public int read(byte b[], int off, int len) throws IOException {
  164.     return readBytes(b, off, len);
  165.     }
  166.  
  167.     /**
  168.      * Reads up to <code>b.length</code> bytes of data from this file 
  169.      * into an array of bytes. This method blocks until at least one byte 
  170.      * of input is available. 
  171.      *
  172.      * @param      b   the buffer into which the data is read.
  173.      * @return     the total number of bytes read into the buffer, or
  174.      *             <code>-1</code> if there is no more data because the end of
  175.      *             this file has been reached.
  176.      * @exception  IOException  if an I/O error occurs.
  177.      * @since      JDK1.0 
  178.      */
  179.     public int read(byte b[]) throws IOException {
  180.     return readBytes(b, 0, b.length);
  181.     }
  182.    
  183.     /**
  184.      * Reads <code>b.length</code> bytes from this file into the byte 
  185.      * array. This method reads repeatedly from the file until all the 
  186.      * bytes are read. This method blocks until all the bytes are read, 
  187.      * the end of the stream is detected, or an exception is thrown. 
  188.      *
  189.      * @param      b   the buffer into which the data is read.
  190.      * @exception  EOFException  if this file reaches the end before reading
  191.      *               all the bytes.
  192.      * @exception  IOException   if an I/O error occurs.
  193.      * @since      JDK1.0
  194.      */
  195.     public final void readFully(byte b[]) throws IOException {
  196.     readFully(b, 0, b.length);
  197.     }
  198.  
  199.     /**
  200.      * Reads exactly <code>len</code> bytes from this file into the byte 
  201.      * array. This method reads repeatedly from the file until all the 
  202.      * bytes are read. This method blocks until all the bytes are read, 
  203.      * the end of the stream is detected, or an exception is thrown. 
  204.      *
  205.      * @param      b     the buffer into which the data is read.
  206.      * @param      off   the start offset of the data.
  207.      * @param      len   the number of bytes to read.
  208.      * @exception  EOFException  if this file reaches the end before reading
  209.      *               all the bytes.
  210.      * @exception  IOException   if an I/O error occurs.
  211.      * @since      JDK1.0
  212.      */
  213.     public final void readFully(byte b[], int off, int len) throws IOException {
  214.     int n = 0;
  215.     while (n < len) {
  216.         int count = this.read(b, off + n, len - n);
  217.         if (count < 0)
  218.         throw new EOFException();
  219.         n += count;
  220.     }
  221.     }
  222.  
  223.     /**
  224.      * Skips exactly <code>n</code> bytes of input. 
  225.      * <p>
  226.      * This method blocks until all the bytes are skipped, the end of 
  227.      * the stream is detected, or an exception is thrown. 
  228.      *
  229.      * @param      n   the number of bytes to be skipped.
  230.      * @return     the number of bytes skipped, which is always <code>n</code>.
  231.      * @exception  EOFException  if this file reaches the end before skipping
  232.      *               all the bytes.
  233.      * @exception  IOException  if an I/O error occurs.
  234.      * @since      JDK1.0
  235.      */
  236.     public int skipBytes(int n) throws IOException {
  237.         seek(getFilePointer() + n);
  238.         return n;
  239.     }
  240.  
  241.     // 'Write' primitives
  242.  
  243.     /**
  244.      * Writes the specified byte to this file. 
  245.      *
  246.      * @param      b   the <code>byte</code> to be written.
  247.      * @exception  IOException  if an I/O error occurs.
  248.      * @since      JDK1.0
  249.      */
  250.     public native void write(int b) throws IOException;
  251.  
  252.     /**
  253.      * Writes a sub array as a sequence of bytes. 
  254.      * @param b the data to be written
  255.      * @param off the start offset in the data
  256.      * @param len the number of bytes that are written
  257.      * @exception IOException If an I/O error has occurred.
  258.      */
  259.     private native void writeBytes(byte b[], int off, int len) throws IOException;
  260.  
  261.     /**
  262.      * Writes <code>b.length</code> bytes from the specified byte array 
  263.      * starting at offset <code>off</code> to this file. 
  264.      *
  265.      * @param      b   the data.
  266.      * @exception  IOException  if an I/O error occurs.
  267.      * @since      JDK1.0
  268.      */
  269.     public void write(byte b[]) throws IOException {
  270.     writeBytes(b, 0, b.length); 
  271.     }
  272.  
  273.     /**
  274.      * Writes <code>len</code> bytes from the specified byte array 
  275.      * starting at offset <code>off</code> to this file. 
  276.      *
  277.      * @param      b     the data.
  278.      * @param      off   the start offset in the data.
  279.      * @param      len   the number of bytes to write.
  280.      * @exception  IOException  if an I/O error occurs.
  281.      * @since      JDK1.0
  282.      */
  283.     public void write(byte b[], int off, int len) throws IOException {
  284.     writeBytes(b, off, len);
  285.     }
  286.  
  287.     // 'Random access' stuff
  288.  
  289.     /**
  290.      * Returns the current offset in this file. 
  291.      *
  292.      * @return     the offset from the beginning of the file, in bytes,
  293.      *             at which the next read or write occurs.
  294.      * @exception  IOException  if an I/O error occurs.
  295.      * @since      JDK1.0
  296.      */
  297.     public native long getFilePointer() throws IOException;
  298.  
  299.     /**
  300.      * Sets the file-pointer offset, measured from the beginning of this 
  301.      * file, at which the next read or write occurs.  The offset may be 
  302.      * set beyond the end of the file. Setting the offset beyond the end 
  303.      * of the file does not change the file length.  The file length will 
  304.      * change only by writing after the offset has been set beyond the end 
  305.      * of the file. 
  306.      *
  307.      * @param      pos   the offset position, measured in bytes from the 
  308.      *                   beginning of the file, at which to set the file 
  309.      *                   pointer.
  310.      * @exception  IOException  if an I/O error occurs.
  311.      * @since      JDK1.0
  312.      */
  313.     public native void seek(long pos) throws IOException;
  314.  
  315.     /**
  316.      * Returns the length of this file.
  317.      *
  318.      * @return     the length of this file.
  319.      * @exception  IOException  if an I/O error occurs.
  320.      * @since      JDK1.0
  321.      */
  322.     public native long length() throws IOException;
  323.  
  324.     /**
  325.      * Closes this random access file stream and releases any system 
  326.      * resources associated with the stream. 
  327.      *
  328.      * @exception  IOException  if an I/O error occurs.
  329.      * @since      JDK1.0
  330.      */
  331.     public native void close() throws IOException;
  332.  
  333.     //
  334.     //  Some "reading/writing Java data types" methods stolen from
  335.     //  DataInputStream and DataOutputStream.
  336.     //
  337.  
  338.     /**
  339.      * Reads a <code>boolean</code> from this file. This method reads a 
  340.      * single byte from the file. A value of <code>0</code> represents 
  341.      * <code>false</code>. Any other value represents <code>true</code>. 
  342.      * This method blocks until the byte is read, the end of the stream 
  343.      * is detected, or an exception is thrown. 
  344.      *
  345.      * @return     the <code>boolean</code> value read.
  346.      * @exception  EOFException  if this file has reached the end.
  347.      * @exception  IOException   if an I/O error occurs.
  348.      * @since      JDK1.0
  349.      */
  350.     public final boolean readBoolean() throws IOException {
  351.     int ch = this.read();
  352.     if (ch < 0)
  353.         throw new EOFException();
  354.     return (ch != 0);
  355.     }
  356.  
  357.     /**
  358.      * Reads a signed 8-bit value from this file. This method reads a 
  359.      * byte from the file. If the byte read is <code>b</code>, where 
  360.      * <code>0 <= b <= 255</code>, 
  361.      * then the result is:
  362.      * <ul><code>
  363.      *     (byte)(b)
  364.      *</code></ul>
  365.      * <p>
  366.      * This method blocks until the byte is read, the end of the stream 
  367.      * is detected, or an exception is thrown. 
  368.      *
  369.      * @return     the next byte of this file as a signed 8-bit
  370.      *             <code>byte</code>.
  371.      * @exception  EOFException  if this file has reached the end.
  372.      * @exception  IOException   if an I/O error occurs.
  373.      * @since      JDK1.0
  374.      */
  375.     public final byte readByte() throws IOException {
  376.     int ch = this.read();
  377.     if (ch < 0)
  378.         throw new EOFException();
  379.     return (byte)(ch);
  380.     }
  381.  
  382.     /**
  383.      * Reads an unsigned 8-bit number from this file. This method reads 
  384.      * a byte from this file and returns that byte. 
  385.      * <p>
  386.      * This method blocks until the byte is read, the end of the stream 
  387.      * is detected, or an exception is thrown. 
  388.      *
  389.      * @return     the next byte of this file, interpreted as an unsigned
  390.      *             8-bit number.
  391.      * @exception  EOFException  if this file has reached the end.
  392.      * @exception  IOException   if an I/O error occurs.
  393.      * @since      JDK1.0
  394.      */
  395.     public final int readUnsignedByte() throws IOException {
  396.     int ch = this.read();
  397.     if (ch < 0)
  398.         throw new EOFException();
  399.     return ch;
  400.     }
  401.  
  402.     /**
  403.      * Reads a signed 16-bit number from this file. The method reads 2 
  404.      * bytes from this file. If the two bytes read, in order, are 
  405.      * <code>b1</code> and <code>b2</code>, where each of the two values is 
  406.      * between <code>0</code> and <code>255</code>, inclusive, then the 
  407.      * result is equal to:
  408.      * <ul><code>
  409.      *     (short)((b1 << 8) | b2)
  410.      * </code></ul>
  411.      * <p>
  412.      * This method blocks until the two bytes are read, the end of the 
  413.      * stream is detected, or an exception is thrown. 
  414.      *
  415.      * @return     the next two bytes of this file, interpreted as a signed
  416.      *             16-bit number.
  417.      * @exception  EOFException  if this file reaches the end before reading
  418.      *               two bytes.
  419.      * @exception  IOException   if an I/O error occurs.
  420.      * @since      JDK1.0
  421.      */
  422.     public final short readShort() throws IOException {
  423.     int ch1 = this.read();
  424.     int ch2 = this.read();
  425.     if ((ch1 | ch2) < 0)
  426.          throw new EOFException();
  427.     return (short)((ch1 << 8) + (ch2 << 0));
  428.     }
  429.  
  430.     /**
  431.      * Reads an unsigned 16-bit number from this file. This method reads 
  432.      * two bytes from the file. If the bytes read, in order, are 
  433.      * <code>b1</code> and <code>b2</code>, where 
  434.      * <code>0 <= b1, b2 <= 255</code>, 
  435.      * then the result is equal to:
  436.      * <ul><code>
  437.      *     (b1 << 8) | b2
  438.      * </code></ul>
  439.      * <p>
  440.      * This method blocks until the two bytes are read, the end of the 
  441.      * stream is detected, or an exception is thrown. 
  442.      *
  443.      * @return     the next two bytes of this file, interpreted as an unsigned
  444.      *             16-bit integer.
  445.      * @exception  EOFException  if this file reaches the end before reading
  446.      *               two bytes.
  447.      * @exception  IOException   if an I/O error occurs.
  448.      * @since      JDK1.0
  449.      */
  450.     public final int readUnsignedShort() throws IOException {
  451.     int ch1 = this.read();
  452.     int ch2 = this.read();
  453.     if ((ch1 | ch2) < 0)
  454.          throw new EOFException();
  455.     return (ch1 << 8) + (ch2 << 0);
  456.     }
  457.  
  458.     /**
  459.      * Reads a Unicode character from this file. This method reads two
  460.      * bytes from the file. If the bytes read, in order, are 
  461.      * <code>b1</code> and <code>b2</code>, where 
  462.      * <code>0 <= b1, b2 <= 255</code>, 
  463.      * then the result is equal to:
  464.      * <ul><code>
  465.      *     (char)((b1 << 8) | b2)
  466.      * </code></ul>
  467.      * <p>
  468.      * This method blocks until the two bytes are read, the end of the 
  469.      * stream is detected, or an exception is thrown. 
  470.      *
  471.      * @return     the next two bytes of this file as a Unicode character.
  472.      * @exception  EOFException  if this file reaches the end before reading
  473.      *               two bytes.
  474.      * @exception  IOException   if an I/O error occurs.
  475.      * @since      JDK1.0
  476.      */
  477.     public final char readChar() throws IOException {
  478.     int ch1 = this.read();
  479.     int ch2 = this.read();
  480.     if ((ch1 | ch2) < 0)
  481.          throw new EOFException();
  482.     return (char)((ch1 << 8) + (ch2 << 0));
  483.     }
  484.  
  485.     /**
  486.      * Reads a signed 32-bit integer from this file. This method reads 4 
  487.      * bytes from the file. If the bytes read, in order, are <code>b1</code>,
  488.      * <code>b2</code>, <code>b3</code>, and <code>b4</code>, where 
  489.      * <code>0 <= b1, b2, b3, b4 <= 255</code>, 
  490.      * then the result is equal to:
  491.      * <ul><code>
  492.      *     (b1 << 24) | (b2 << 16) + (b3 << 8) + b4
  493.      * </code></ul>
  494.      * <p>
  495.      * This method blocks until the four bytes are read, the end of the 
  496.      * stream is detected, or an exception is thrown. 
  497.      *
  498.      * @return     the next four bytes of this file, interpreted as an
  499.      *             <code>int</code>.
  500.      * @exception  EOFException  if this file reaches the end before reading
  501.      *               four bytes.
  502.      * @exception  IOException   if an I/O error occurs.
  503.      * @since      JDK1.0
  504.      */
  505.     public final int readInt() throws IOException {
  506.     int ch1 = this.read();
  507.     int ch2 = this.read();
  508.     int ch3 = this.read();
  509.     int ch4 = this.read();
  510.     if ((ch1 | ch2 | ch3 | ch4) < 0)
  511.          throw new EOFException();
  512.     return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0));
  513.     }
  514.  
  515.     /**
  516.      * Reads a signed 64-bit integer from this file. This method reads eight
  517.      * bytes from the file. If the bytes read, in order, are 
  518.      * <code>b1</code>, <code>b2</code>, <code>b3</code>, 
  519.      * <code>b4</code>, <code>b5</code>, <code>b6</code>, 
  520.      * <code>b7</code>, and <code>b8,</code> where:
  521.      * <ul><code>
  522.      *     0 <= b1, b2, b3, b4, b5, b6, b7, b8 <=255,
  523.      * </code></ul>
  524.      * <p>
  525.      * then the result is equal to:
  526.      * <p><blockquote><pre>
  527.      *     ((long)b1 << 56) + ((long)b2 << 48)
  528.      *     + ((long)b3 << 40) + ((long)b4 << 32)
  529.      *     + ((long)b5 << 24) + ((long)b6 << 16)
  530.      *     + ((long)b7 << 8) + b8
  531.      * </pre></blockquote>
  532.      * <p>
  533.      * This method blocks until the eight bytes are read, the end of the 
  534.      * stream is detected, or an exception is thrown. 
  535.      *
  536.      * @return     the next eight bytes of this file, interpreted as a
  537.      *             <code>long</code>.
  538.      * @exception  EOFException  if this file reaches the end before reading
  539.      *               eight bytes.
  540.      * @exception  IOException   if an I/O error occurs.
  541.      * @since      JDK1.0
  542.      */
  543.     public final long readLong() throws IOException {
  544.     return ((long)(readInt()) << 32) + (readInt() & 0xFFFFFFFFL);
  545.     }
  546.  
  547.     /**
  548.      * Reads a <code>float</code> from this file. This method reads an 
  549.      * <code>int</code> value as if by the <code>readInt</code> method 
  550.      * and then converts that <code>int</code> to a <code>float</code> 
  551.      * using the <code>intBitsToFloat</code> method in class 
  552.      * <code>Float</code>. 
  553.      * <p>
  554.      * This method blocks until the four bytes are read, the end of the 
  555.      * stream is detected, or an exception is thrown. 
  556.      *
  557.      * @return     the next four bytes of this file, interpreted as a
  558.      *             <code>float</code>.
  559.      * @exception  EOFException  if this file reaches the end before reading
  560.      *             four bytes.
  561.      * @exception  IOException   if an I/O error occurs.
  562.      * @see        java.io.RandomAccessFile#readInt()
  563.      * @see        java.lang.Float#intBitsToFloat(int)
  564.      * @since      JDK1.0
  565.      */
  566.     public final float readFloat() throws IOException {
  567.     return Float.intBitsToFloat(readInt());
  568.     }
  569.  
  570.     /**
  571.      * Reads a <code>double</code> from this file. This method reads a 
  572.      * <code>long</code> value as if by the <code>readLong</code> method 
  573.      * and then converts that <code>long</code> to a <code>double</code> 
  574.      * using the <code>longBitsToDouble</code> method in 
  575.      * class <code>Double</code>.
  576.      * <p>
  577.      * This method blocks until the eight bytes are read, the end of the 
  578.      * stream is detected, or an exception is thrown. 
  579.      *
  580.      * @return     the next eight bytes of this file, interpreted as a
  581.      *             <code>double</code>.
  582.      * @exception  EOFException  if this file reaches the end before reading
  583.      *             eight bytes.
  584.      * @exception  IOException   if an I/O error occurs.
  585.      * @see        java.io.RandomAccessFile#readLong()
  586.      * @see        java.lang.Double#longBitsToDouble(long)
  587.      * @since      JDK1.0
  588.      */
  589.     public final double readDouble() throws IOException {
  590.     return Double.longBitsToDouble(readLong());
  591.     }
  592.  
  593.     /**
  594.      * Reads the next line of text from this file. This method 
  595.      * successively reads bytes from the file until it reaches the end of 
  596.      * a line of text. 
  597.      * <p>
  598.      * A line of text is terminated by a carriage-return character 
  599.      * (<code>'\r'</code>), a newline character (<code>'\n'</code>), a 
  600.      * carriage-return character immediately followed by a newline 
  601.      * character, or the end of the input stream. The line-terminating 
  602.      * character(s), if any, are included as part of the string returned. 
  603.      * <p>
  604.      * This method blocks until a newline character is read, a carriage 
  605.      * return and the byte following it are read (to see if it is a 
  606.      * newline), the end of the stream is detected, or an exception is thrown.
  607.      *
  608.      * @return     the next line of text from this file.
  609.      * @exception  IOException  if an I/O error occurs.
  610.      * @since      JDK1.0
  611.      */
  612.     public final String readLine() throws IOException {
  613.     StringBuffer input = new StringBuffer();
  614.     int c;
  615.  
  616.     while (((c = read()) != -1) && (c != '\n')) {
  617.         input.append((char)c);
  618.     }
  619.     if ((c == -1) && (input.length() == 0)) {
  620.         return null;
  621.     }
  622.     return input.toString();
  623.     }
  624.  
  625.     /**
  626.      * Reads in a string from this file. The string has been encoded 
  627.      * using a modified UTF-8 format. 
  628.      * <p>
  629.      * The first two bytes are read as if by 
  630.      * <code>readUnsignedShort</code>. This value gives the number of 
  631.      * following bytes that are in the encoded string, not
  632.      * the length of the resulting string. The following bytes are then 
  633.      * interpreted as bytes encoding characters in the UTF-8 format 
  634.      * and are converted into characters. 
  635.      * <p>
  636.      * This method blocks until all the bytes are read, the end of the 
  637.      * stream is detected, or an exception is thrown. 
  638.      *
  639.      * @return     a Unicode string.
  640.      * @exception  EOFException            if this file reaches the end before
  641.      *               reading all the bytes.
  642.      * @exception  IOException             if an I/O error occurs.
  643.      * @exception  UTFDataFormatException  if the bytes do not represent 
  644.      *               valid UTF-8 encoding of a Unicode string.
  645.      * @see        java.io.RandomAccessFile#readUnsignedShort()
  646.      * @since      JDK1.0
  647.      */
  648.     public final String readUTF() throws IOException {
  649.     return DataInputStream.readUTF(this);
  650.     }
  651.  
  652.     /**
  653.      * Writes a <code>boolean</code> to the file as a 1-byte value. The 
  654.      * value <code>true</code> is written out as the value 
  655.      * <code>(byte)1</code>; the value <code>false</code> is written out 
  656.      * as the value <code>(byte)0</code>.
  657.      *
  658.      * @param      v   a <code>boolean</code> value to be written.
  659.      * @exception  IOException  if an I/O error occurs.
  660.      * @since      JDK1.0
  661.      */
  662.     public final void writeBoolean(boolean v) throws IOException {
  663.     write(v ? 1 : 0);
  664.     //written++;
  665.     }
  666.  
  667.     /**
  668.      * Writes a <code>byte</code> to the file as a 1-byte value. 
  669.      *
  670.      * @param      v   a <code>byte</code> value to be written.
  671.      * @exception  IOException  if an I/O error occurs.
  672.      * @since      JDK1.0
  673.      */
  674.     public final void writeByte(int v) throws IOException {
  675.     write(v);
  676.     //written++;
  677.     }
  678.  
  679.     /**
  680.      * Writes a <code>short</code> to the file as two bytes, high byte first.
  681.      *
  682.      * @param      v   a <code>short</code> to be written.
  683.      * @exception  IOException  if an I/O error occurs.
  684.      * @since      JDK1.0
  685.      */
  686.     public final void writeShort(int v) throws IOException {
  687.     write((v >>> 8) & 0xFF);
  688.     write((v >>> 0) & 0xFF);
  689.     //written += 2;
  690.     }
  691.  
  692.     /**
  693.      * Writes a <code>char</code> to the file as a 2-byte value, high
  694.      * byte first.
  695.      *
  696.      * @param      v   a <code>char</code> value to be written.
  697.      * @exception  IOException  if an I/O error occurs.
  698.      * @since      JDK1.0
  699.      */
  700.     public final void writeChar(int v) throws IOException {
  701.     write((v >>> 8) & 0xFF);
  702.     write((v >>> 0) & 0xFF);
  703.     //written += 2;
  704.     }
  705.  
  706.     /**
  707.      * Writes an <code>int</code> to the file as four bytes, high byte first.
  708.      *
  709.      * @param      v   an <code>int</code> to be written.
  710.      * @exception  IOException  if an I/O error occurs.
  711.      * @since      JDK1.0
  712.      */
  713.     public final void writeInt(int v) throws IOException {
  714.     write((v >>> 24) & 0xFF);
  715.     write((v >>> 16) & 0xFF);
  716.     write((v >>>  8) & 0xFF);
  717.     write((v >>>  0) & 0xFF);
  718.     //written += 4;
  719.     }
  720.  
  721.     /**
  722.      * Writes a <code>long</code> to the file as eight bytes, high byte first.
  723.      *
  724.      * @param      v   a <code>long</code> to be written.
  725.      * @exception  IOException  if an I/O error occurs.
  726.      * @since      JDK1.0
  727.      */
  728.     public final void writeLong(long v) throws IOException {
  729.     write((int)(v >>> 56) & 0xFF);
  730.     write((int)(v >>> 48) & 0xFF);
  731.     write((int)(v >>> 40) & 0xFF);
  732.     write((int)(v >>> 32) & 0xFF);
  733.     write((int)(v >>> 24) & 0xFF);
  734.     write((int)(v >>> 16) & 0xFF);
  735.     write((int)(v >>>  8) & 0xFF);
  736.     write((int)(v >>>  0) & 0xFF);
  737.     //written += 8;
  738.     }
  739.  
  740.     /**
  741.      * Converts the float argument to an <code>int</code> using the 
  742.      * <code>floatToIntBits</code> method in class <code>Float</code>, 
  743.      * and then writes that <code>int</code> value to the file as a 
  744.      * 4-byte quantity, high byte first. 
  745.      *
  746.      * @param      v   a <code>float</code> value to be written.
  747.      * @exception  IOException  if an I/O error occurs.
  748.      * @see        java.lang.Float#floatToIntBits(float)
  749.      * @since      JDK1.0
  750.      */
  751.     public final void writeFloat(float v) throws IOException {
  752.     writeInt(Float.floatToIntBits(v));
  753.     }
  754.  
  755.     /**
  756.      * Converts the double argument to a <code>long</code> using the 
  757.      * <code>doubleToLongBits</code> method in class <code>Double</code>, 
  758.      * and then writes that <code>long</code> value to the file as an 
  759.      * 8-byte quantity, high byte first. 
  760.      *
  761.      * @param      v   a <code>double</code> value to be written.
  762.      * @exception  IOException  if an I/O error occurs.
  763.      * @see        java.lang.Double#doubleToLongBits(double)
  764.      * @since      JDK1.0
  765.      */
  766.     public final void writeDouble(double v) throws IOException {
  767.     writeLong(Double.doubleToLongBits(v));
  768.     }
  769.  
  770.     /**
  771.      * Writes the string to the file as a sequence of bytes. Each 
  772.      * character in the string is written out, in sequence, by discarding 
  773.      * its high eight bits. 
  774.      *
  775.      * @param      s   a string of bytes to be written.
  776.      * @exception  IOException  if an I/O error occurs.
  777.      * @since      JDK1.0
  778.      */
  779.     public final void writeBytes(String s) throws IOException {
  780.     int len = s.length();
  781.     byte[] b = new byte[len];
  782.     s.getBytes(0, len, b, 0);
  783.     writeBytes(b, 0, len);
  784.     }
  785.  
  786.     /**
  787.      * Writes a string to the file as a sequence of characters. Each 
  788.      * character is written to the data output stream as if by the 
  789.      * <code>writeChar</code> method. 
  790.      *
  791.      * @param      s   a <code>String</code> value to be written.
  792.      * @exception  IOException  if an I/O error occurs.
  793.      * @see        java.io.RandomAccessFile#writeChar(int)
  794.      * @since      JDK1.0
  795.      */
  796.     public final void writeChars(String s) throws IOException {
  797.     int clen = s.length();
  798.     int blen = 2*clen;
  799.     byte[] b = new byte[blen];
  800.     char[] c = new char[clen];
  801.     s.getChars(0, clen, c, 0);
  802.     for (int i = 0, j = 0; i < clen; i++) {
  803.         b[j++] = (byte)(c[i] >>> 8);
  804.         b[j++] = (byte)(c[i] >>> 0);
  805.     }
  806.     writeBytes(b, 0, blen);
  807.     }
  808.  
  809.     /**
  810.      * Writes a string to the file using UTF-8 encoding in a 
  811.      * machine-independent manner. 
  812.      * <p>
  813.      * First, two bytes are written to the file as if by the 
  814.      * <code>writeShort</code> method giving the number of bytes to 
  815.      * follow. This value is the number of bytes actually written out, 
  816.      * not the length of the string. Following the length, each character 
  817.      * of the string is output, in sequence, using the UTF-8 encoding 
  818.      * for each character. 
  819.      *
  820.      * @param      str   a string to be written.
  821.      * @exception  IOException  if an I/O error occurs.
  822.      * @since      JDK1.0
  823.      */
  824.     public final void writeUTF(String str) throws IOException {
  825.     int strlen = str.length();
  826.     int utflen = 0;
  827.  
  828.     for (int i = 0 ; i < strlen ; i++) {
  829.         int c = str.charAt(i);
  830.         if ((c >= 0x0001) && (c <= 0x007F)) {
  831.         utflen++;
  832.         } else if (c > 0x07FF) {
  833.         utflen += 3;
  834.         } else {
  835.         utflen += 2;
  836.         }
  837.     }
  838.  
  839.     if (utflen > 65535)
  840.         throw new UTFDataFormatException();          
  841.  
  842.     write((utflen >>> 8) & 0xFF);
  843.     write((utflen >>> 0) & 0xFF);
  844.     for (int i = 0 ; i < strlen ; i++) {
  845.         int c = str.charAt(i);
  846.         if ((c >= 0x0001) && (c <= 0x007F)) {
  847.         write(c);
  848.         } else if (c > 0x07FF) {
  849.         write(0xE0 | ((c >> 12) & 0x0F));
  850.         write(0x80 | ((c >>  6) & 0x3F));
  851.         write(0x80 | ((c >>  0) & 0x3F));
  852.         //written += 2;
  853.         } else {
  854.         write(0xC0 | ((c >>  6) & 0x1F));
  855.         write(0x80 | ((c >>  0) & 0x3F));
  856.         //written += 1;
  857.         }
  858.     }
  859.     //written += strlen + 2;
  860.     }
  861. }
  862.